package pers.jsan.litheopen.security.filter;

import pers.jsan.litheopen.security.exception.LitheAuthenticationException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;

public class AuthenticationFilterChain implements LitheFilterChain {

    private Iterator<LitheAuthenticationFilter> filters;

    public AuthenticationFilterChain() {

    }

    public AuthenticationFilterChain(Iterator<LitheAuthenticationFilter> filters) {
        this.filters = filters;
    }

    public void doFilter(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException, ServletException, LitheAuthenticationException {
        if(filters.hasNext()) {
            LitheAuthenticationFilter next = filters.next();
            next.doFilter(servletRequest, servletResponse, this);
        }
    }

    public void setFilters(Iterator<LitheAuthenticationFilter> filters) {
        this.filters = filters;
    }

    public AuthenticationFilterChain copy() throws CloneNotSupportedException {
        return (AuthenticationFilterChain) this.clone();
    }
}
